
#include "History.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string>
#include <conio.h>
#include <windows.h>
#include <stdlib.h>
#include <dirent.h>

using namespace std;

/* Konstruktor */
History::History(){
}

History::History(const string& namafile){
		namaf=namafile;
}

/* Destruktor */
History::~History(){
		namaf="";
		folder="";
}

/* Other Method */
string History::GetName(){	//mengembalikan namafile
		return namaf;
}

Player History::GetPlayer(int i)
{
	return mPlayer[i];
}

void History::initPlayer(string load){
	bool flag;
	LoadHighScore(&flag, load);
	if (flag){
		cout<<endl<<"load berhasil";
	}
	else{	
		for (int i=0;i<=9;i++){
			mPlayer[i].setDefault();
		}
	}
}
void History::SetName(string namafile) {	//menset namafile
		namaf=namafile;
}

string History::GetFolder(){	//mendapatkan folder tempat file
		return folder;
}

void History::SetFolder(int* flag){	//mensetfolder dengan membuka browser dialog
    BROWSEINFO   bi;  //objek dari winAPI
    ZeroMemory(&bi,   sizeof(bi));
	string temp;	//string sementara
	int i;	//iterator
	char ch;
    TCHAR   szDisplayName[MAX_PATH]; //menyimpan alamat path
    //szDisplayName[0]    =   ''";

	bi.hwndOwner        =   NULL;
    bi.pidlRoot         =   NULL;
    bi.pszDisplayName   =   szDisplayName;
    //bi.lpszTitle        =   _T("Please select a folder for storing received files :"); 	//belum jelas
    bi.ulFlags          =   BIF_RETURNONLYFSDIRS;
    //bi.lParam           =   NULL;  //belum jelas
    bi.iImage           =   0;

    LPITEMIDLIST   pidl   =   SHBrowseForFolder(&bi); //membuka browse folder dialog
    TCHAR   szPathName[MAX_PATH];
	if   (NULL   !=   pidl)
    {
         BOOL bRet = SHGetPathFromIDList(pidl,szPathName); //mengambil path item
         if(FALSE == bRet)
              return;
        // AfxMessageBox(szPathName);
    }
	temp=szPathName;
	//cout<<temp<<endl; getch();
	if ((int(temp[0])<31) || (int(temp[0])>126)){	//antisipasi cancel
		*flag=2;
		folder="";
	}else{
		for (int i=0; i<=(int)temp.length(); i++){	//mengubah path ke format yang sesuai
		if (temp[i]=='\\') temp[i]='/';
		};
		folder=temp+'/';
		//cout<<folder<<endl; getch();
		cout<<"Apakah direktori sesuai (y/~y)? ";
		cin>>ch;
		if ((int(ch)==89)||(int(ch)==121)){	//apakah pilihan  'y' atau 'Y'
			(*flag)=0;
			//cout<<GetFolder()<<endl; getch();
		}else{
			folder="";
			szPathName[0]=12;
		};
	};
}

void History::LoadHighScore(bool* flag, string namafile){
	ifstream input;
	string nameTemp, ch, h;
	int timeTemp, coinTemp;
	input.open(namafile.c_str()); //membuka file
	if(!input) // mengecek hasil pembukaan file
	{
		cerr << "File tidak bisa dibuka atau tidak terdapat.\n"; //print 'cannot open file'
		(*flag)=false;	//false berarti load gagal
	} else
	{
		for (int i=0; i<=9; i++){ //membaca data lahan 6*7 petak
				input>>ch; //mengambil satu panjang string sebelum samapai spasi
				cout<<ch;
				nameTemp=ch; //mencacah string -> char* //plantable
				
				input>>ch; //mengambil satu panjang string sebelum samapai spasi
				cout<<ch;
				h = ch[0];
				coinTemp=atoi(h.c_str()); //memasukkan ke penampung sementara, berupa integer

				input>>ch; //mengambil satu panjang string sebelum samapai spasi
				cout<<ch;
				h = ch;
				timeTemp=atoi(h.c_str()); //memasukkan ke penampung sementara, berupa integer

				mPlayer[i].setName(nameTemp);
				mPlayer[i].setTime(timeTemp);
				mPlayer[i].setCoinLeft(coinTemp);
		} //membaca lahan

		//menutup file
		input.close();
		SetName(namafile);
		(*flag)=true;	//false berarti load gagal, true berarti sukses
	}
}

void History::SaveHighScore(string namafile){	//menyimpan ke file
	ofstream output(namafile.c_str()); //membuat file save
	if(!output) // jika gagal membuat (dan membuka) file
	{
		cerr << "Tidak dapat membuat file penyimpanan.\n"; //pesan error
	} else {
		//KAMUS
		int i,j,temp;
		int p,pe,pa,c,s;
		char *ch;
		string kt;
		temp = 1;
		//ALGORITMA
		for (int j=0;j<=9;j++){
			output<<mPlayer[j].getName();
			output<<" ";
			output<<mPlayer[j].getCoinLeft();
			output<<" ";
			output<<mPlayer[j].getTime();
			output<<endl;
		}
		output<<"#";	//memberi tanda akhir
		output.close();	//menutup stream file
		SetName(namafile);
	}
}
void History::SaveLastGame(string namafile, Coin myCoin[24], int modeLoad, int turnLoad, bool enemyLoad, int game){	//menyimpan ke file
	ofstream output(namafile.c_str()); //membuat file save
	if(!output) // jika gagal membuat (dan membuka) file
	{
		cerr << "Tidak dapat membuat file penyimpanan.\n"; //pesan error
	} else {
		//KAMUS
		int i,j,temp;
		int p,pe,pa,c,s;
		char *ch;
		string kt;
		temp = 1;
		//ALGORITMA
		output<<game;
		output<<endl;
		output<<modeLoad;
		output<<endl;
		output<<turnLoad;
		output<<endl;
		if (enemyLoad){
			output<<"1";
		}
		else{
			output<<"0";		
		}
		output<<endl;
		for (int j=0;j<=23;j++){
			output<<myCoin[j].getAlive();
			output<<" ";
			output<<myCoin[j].getStatus();
			output<<" ";
			output<<myCoin[j].getX();
			output<<" ";
			output<<myCoin[j].getY();
			output<<" ";
			output<<endl;
		}
		output<<"#";	//memberi tanda akhir
		output.close();	//menutup stream file
		SetName(namafile);
	}
}

void History::LoadLastGame(bool* flag, string namafile, Coin* myCoin, int* modeLoad, int* turnLoad, bool* enemyLoad){
		ifstream input;
	string nameTemp, ch, h, coinTemp;
	int game, modeTemp, turnTemp, enemyTemp;
	input.open(namafile.c_str()); //membuka file
	if(!input) // mengecek hasil pembukaan file
	{
		cerr << "File tidak bisa dibuka atau tidak terdapat.\n"; //print 'cannot open file'
		(*flag)=false;	//false berarti load gagal
	} else
	{
		input>>ch; //mengambil satu panjang string sebelum samapai spasi
		cout<<"game "<<ch;
		h = ch[0];
		game=atoi(h.c_str()); //memasukkan ke penampung sementara, berupa integer
		if (game==1){ // game tidak usah di load, sudah berakhir
			(*flag)=false;	//false berarti load gagal
		}
		else{
			input>>ch; //mengambil satu panjang string sebelum samapai spasi
			cout<<"mode "<<ch;
			h = ch[0];
			modeTemp=atoi(h.c_str()); //memasukkan ke penampung sementara, berupa integer
			(*modeLoad) = modeTemp;

			input>>ch; //mengambil satu panjang string sebelum samapai spasi
			cout<<"turn "<<ch;
			h = ch[0];
			turnTemp=atoi(h.c_str()); //memasukkan ke penampung sementara, berupa integer
			(*turnLoad) = turnTemp;
			
			input>>ch; //mengambil satu panjang string sebelum samapai spasi
			cout<<"enemy "<<ch;
			h = ch[0];
			enemyTemp=atoi(h.c_str()); //memasukkan ke penampung sementara, berupa integer
			if (enemyTemp==1){
				(*enemyLoad) = true;
			}
			else{
				(*enemyLoad) = false;
			}
			cout<<endl<<"mulai=";
			for (int i=0; i<=23; i++){ //membaca data lahan 6*7 petak
					myCoin[i].setNumber(i+1); //memasukkan ke penampung sementara, berupa integer
					input>>ch; //mengambil satu panjang string sebelum samapai spasi
					cout<<ch;
					h = ch[0];
					myCoin[i].setAlive(atoi(h.c_str())); //memasukkan ke penampung sementara, berupa integer
					input>>ch; //mengambil satu panjang string sebelum samapai spasi
					cout<<ch;
					h = ch[0];
					myCoin[i].setStatus(atoi(h.c_str())); //memasukkan ke penampung sementara, berupa integer
					input>>ch; //mengambil satu panjang string sebelum samapai spasi
					cout<<ch;
					h = ch[0];
					myCoin[i].setX(atoi(h.c_str())); //memasukkan ke penampung sementara, berupa integer
					input>>ch; //mengambil satu panjang string sebelum samapai spasi
					cout<<ch;
					h = ch[0];
					myCoin[i].setY(atoi(h.c_str())); //memasukkan ke penampung sementara, berupa integer
				cout<<endl;

			} //membaca lahan
			(*flag)=true;	//false berarti load gagal, true berarti sukses

		}
		//menutup file
		input.close();
		SetName(namafile);
	}
}

// kerjaan mp
void History::initHighScore(int j)
{
	int i = 0;
	for (i = 0; i < 10; ++i)
	{
		mPlayer[i].setName("tes");
		mPlayer[i].setTime(10* j);
		mPlayer[i].setCoinLeft(i);
	}
}

void History::updateHighScore()
{

}

void History::printcmd()
{
    int i = 0;
	for (i = 0; i < 10; ++i)
	{
		cout<<GetPlayer(i).getName()<<endl;
		cout<<GetPlayer(i).getTime()<<endl;
		cout<<GetPlayer(i).getCoinLeft()<<endl;
	}
}
